//
//  DQBCustomAlertVIew.m
//  DrivingQuestionBank
//
//  Created by mac on 2024/1/10.
//

#import "DQBCustomAlertVIew.h"

@interface DQBCustomAlertVIew ()

@property (nonatomic, strong) UIButton *backgroundButton; //背景
@property (nonatomic, strong) UIImageView *alertImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *leftBtn;
@property (strong, nonatomic) UIButton *rightBtn;

@property (nonatomic ,strong) UIView *verLine; //水平线
@property (nonatomic ,strong) UIView *horLine; //垂直线


@property (nonatomic, strong) NSMutableArray <DQBCustomAlertAction *> *actionArr; // 事件数组

@end

@implementation DQBCustomAlertVIew

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - Life Cycle

- (instancetype)initWithTitle:(NSString *)titleStr
                      leftStr:(nullable NSString *)leftStr  
                     rightStr:(nullable NSString *)rightStr
                   alertImage:(nullable UIImage *)alertImage{
    self = [super init];
    if (self) {
        self.titleLabel.text = titleStr;
        self.alertImageView.image = alertImage ? alertImage : IMAGE_NAMED(@"wrong");
        [self.leftBtn setTitle:leftStr forState:UIControlStateNormal];
        [self.rightBtn setTitle:rightStr forState:UIControlStateNormal];
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 15.f;
    }
    return self;
}

- (void)initAlertSubView{
    self.backgroundButton.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    CGFloat containW = ScreenWidth * 0.8;
    CGFloat containH = ScreenHeight * 0.3;
    self.backgroundColor = [UIColor whiteColor];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(containW);
        make.height.mas_equalTo(containH);
    }];
    
    [self addSubview:self.alertImageView];
    [self.alertImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.centerX.mas_equalTo(0);
        make.width.height.mas_equalTo(containH*0.4);
    }];
    
    [self addSubview:[self titleLabel]];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(20);
        make.top.mas_equalTo(self.alertImageView.mas_bottom).offset(30);
    }];
    
    [self addSubview:[self horLine]];
    [_horLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(-50);
    }];
    [self addSubview:[self verLine]];
    [_verLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(51);
        make.width.mas_equalTo(0.5);
        make.centerX.mas_equalTo(0);
    }];

    [self addSubview:[self leftBtn]];
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(containW/2);
        make.bottom.mas_equalTo(0);
    }];
    [self addSubview:[self rightBtn]];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(containW/2);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)show {
    [KEYWINDOW addSubview:self.backgroundButton];
    [KEYWINDOW addSubview:self];
    [self initAlertSubView];
    
}

- (void)addAction:(DQBCustomAlertAction *)action{
    [self.actionArr addObject:action];
}


- (void)disMiss {
    [self.backgroundButton removeFromSuperview];
    [self removeFromSuperview];
}

- (void)leftAction{
    DQBCustomAlertAction *block = [self.actionArr firstObject];
    block.actionBlock(block.actionTitle);
}

- (void)rightAction{
    DQBCustomAlertAction *block = [self.actionArr lastObject];
    block.actionBlock(block.actionTitle);
}

#pragma mark - Lazy

- (UIButton *)backgroundButton{
    
    if (!_backgroundButton) {
        _backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _backgroundButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }
    return _backgroundButton;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = SYSTEM_FONT(22.5);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = DQBRGBHex(0x232b36);
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UIImageView *)alertImageView{
    
    if (!_alertImageView) {
        _alertImageView = [UIImageView new];
        _alertImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _alertImageView;
}

- (UIButton *)leftBtn{
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftBtn.contentMode = UIViewContentModeScaleAspectFill;
        [_leftBtn setTitleColor:DQBRGBHex(0x232b36) forState:UIControlStateNormal];
        [_leftBtn addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
        _leftBtn.titleLabel.font = SYSTEM_FONT(18);
    }
    return _leftBtn;
}

- (UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.contentMode = UIViewContentModeScaleAspectFill;
        [_rightBtn setTitleColor:DQBRGBHex(0x232b36) forState:UIControlStateNormal];
        [_rightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
        _rightBtn.titleLabel.font = SYSTEM_FONT(18);
    }
    return _rightBtn;
}

- (UIView *)verLine{
    if (!_verLine) {
        _verLine = [[UIView alloc] init];
        _verLine.backgroundColor = DQBRGBHex(0x555555);
    }
    return _verLine;
}

- (UIView *)horLine{
    if (!_horLine) {
        _horLine = [[UIView alloc] init];
        _horLine.backgroundColor = DQBRGBHex(0x555555);
    }
    return _horLine;
}

- (NSMutableArray *)actionArr{
    if (!_actionArr) {
        _actionArr = [NSMutableArray array];
    }
    return _actionArr;
}

@end


@interface DQBCustomAlertAction ()

@property (nonatomic, copy) NSString *actionTitle;
@property (nonatomic, assign) AlertActionType actionType;

@end

@implementation DQBCustomAlertAction

+ (instancetype _Nullable)initActionWithTitle:(nullable NSString *)title AlertActionType:(AlertActionType)actionType BlockAction:(void (^ _Nullable __strong)(NSString * _Nonnull __strong))blockAction {
    DQBCustomAlertAction *alertAction = [DQBCustomAlertAction new];
    alertAction.actionTitle = title;
    alertAction.actionType = actionType;
    alertAction.actionBlock = blockAction;
    return alertAction;
}

@end
